package com.lagou.sqlSession;

import com.lagou.connect.ConectionManager;
import com.lagou.connect.DefaultConectionManager;
import com.lagou.connect.TransactionIsolationLevel;
import com.lagou.pojo.Configuration;

public class DefaultSqlSessionFactory implements SqlSessionFactory {

    private Configuration configuration;

    public DefaultSqlSessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }


    @Override
    public SqlSession openSession() {
        return openSession(true);
    }

    @Override
    public SqlSession openSession(boolean autoCommit) {
        return openSession(null,autoCommit);
    }

    @Override
    public SqlSession openSession(TransactionIsolationLevel level, boolean autoCommit) {
        ConectionManager conectionManager = new DefaultConectionManager(configuration.getDataSource(),level, autoCommit);
        Executor executor = new SimpleExecutor(conectionManager);
        return new DefaultSqlSession(configuration,executor);
    }

    @Override
    public Configuration getConfiguration() {
        return configuration;
    }
}
